//
//  PreferencesData.m
//

#import "PreferencesData.h"

#import "Utility.h"


#define PREF_CONFIRM_TRASH			@"confirmTrash"
#define PREF_USE_APPLE_REMOTE		@"useAppleRemote"
#define PREF_STARTUP_WINDOW_TYPE	@"startupWindowType"
#define PREF_STARTUP_FOLDER_TYPE	@"startupFolderType"
#define PREF_STARTUP_FOLDER			@"startupFolder"

#define PREF_REGEN_THUMBNAILS		@"regenerateThumbnails"
#define PREF_MARK_GEN_THUMBNAILS	@"markGenThumbnails"
#define PREF_BROWSER_SHOW_HIDDEN	@"showHiddenFolders"
#define PREF_BROWSER_BACK_COLOR		@"browserBackColor"
#define PREF_BROWSER_TEXT_COLOR		@"browserTextColor"

#define PREF_VIEWER_PAGE_JUMP		@"pageJumpSize"
#define PREF_VIEWER_PATH_ELEMENTS	@"pathElements"
#define PREF_VIEWER_BACK_COLOR		@"viewerBackColor"
#define PREF_VIEWER_TEXT_COLOR		@"viewerTextColor"
#define PREF_VIEWER_SINGLE_WINDOW	@"useSingleWindow"
#define PREF_VIEWER_MOVIE_SIZE		@"useMovieNaturalSize"
#define PREF_VIEWER_THUMB_STRIP_STATE	@"thumbStripState"

#define PREF_SLIDESHOW_DELAY		@"slideshowDelay"
#define PREF_SLIDESHOW_ORDER		@"slideshowOrder"
#define PREF_SLIDESHOW_BACK_COLOR	@"slideshowBackColor"
#define PREF_SLIDESHOW_TEXT_COLOR	@"slideshowTextColor"
#define PREF_SLIDESHOW_FOLDER		@"slideshowFolder"



NSString *PBPrefencesDidChangeNotification = @"PBPrefencesDidChangeNotification";



@implementation PreferencesData

//------------------------------------------------------------------------------
- (id) initWithDefaults: (NSUserDefaults *)defaults
{
	self = [super init];
	if (nil != self)
	{
		// General
		confirmMoveToTrash = YES;
		getBoolFromPreferences(defaults, PREF_CONFIRM_TRASH, &confirmMoveToTrash);
		useAppleRemote = YES;
		getBoolFromPreferences(defaults, PREF_USE_APPLE_REMOTE, &useAppleRemote);
		startupWindowType = getIntFromPreferences(
			defaults, PREF_STARTUP_WINDOW_TYPE, STARTUP_WIN_BROWSER);
		startupFolderType = getIntFromPreferences(
			defaults, PREF_STARTUP_FOLDER_TYPE, STARTUP_FOLDER_LAST);
		startupFolder = getStringFromPreferences(defaults, PREF_STARTUP_FOLDER, NULL);
		[startupFolder retain];


		// Browser
		regenerateThumbnails = YES;
		markGeneratedThumbnails = YES;
		showHiddenFolders = NO;
		getBoolFromPreferences(defaults, PREF_REGEN_THUMBNAILS, &regenerateThumbnails);
		getBoolFromPreferences(defaults, PREF_MARK_GEN_THUMBNAILS, &markGeneratedThumbnails);
		getBoolFromPreferences(defaults, PREF_BROWSER_SHOW_HIDDEN, &showHiddenFolders);
		browserBackgroundColor = getColorFromPreferences(defaults, PREF_BROWSER_BACK_COLOR, nil);
		[browserBackgroundColor retain];
		browserTextColor = getColorFromPreferences(defaults, PREF_BROWSER_TEXT_COLOR, nil);
		[browserTextColor retain];


		// Viewer
		useSingleWindow = YES;
		useMovieNaturalSize = NO;
		getBoolFromPreferences(defaults, PREF_VIEWER_SINGLE_WINDOW, &useSingleWindow);
		getBoolFromPreferences(defaults, PREF_VIEWER_MOVIE_SIZE, &useMovieNaturalSize);
		pageJump = getIntFromPreferences(defaults, PREF_VIEWER_PAGE_JUMP, 10);
		pathElements = getIntFromPreferences(defaults, PREF_VIEWER_PATH_ELEMENTS, 0);
		thumbStripState = getIntFromPreferences(defaults, PREF_VIEWER_THUMB_STRIP_STATE, 1);
		viewerBackgroundColor = getColorFromPreferences(defaults, PREF_VIEWER_BACK_COLOR, nil);
		[viewerBackgroundColor retain];
		viewerTextColor = getColorFromPreferences(defaults, PREF_VIEWER_TEXT_COLOR, nil);
		[viewerTextColor retain];


		// Slideshow
		slideshowDelay = getIntFromPreferences(defaults, PREF_SLIDESHOW_DELAY, 10);
		slideshowOrder = getIntFromPreferences(defaults, PREF_SLIDESHOW_ORDER, 0);
		slideshowBackgroundColor = getColorFromPreferences(defaults, PREF_SLIDESHOW_BACK_COLOR, nil);
		[slideshowBackgroundColor retain];
		slideshowTextColor = getColorFromPreferences(defaults, PREF_SLIDESHOW_TEXT_COLOR, nil);
		[slideshowTextColor retain];
		slideshowFolder = getStringFromPreferences(defaults, PREF_SLIDESHOW_FOLDER, GetPicturesFolder());
		[slideshowFolder retain];
	}

	return self;
}

//------------------------------------------------------------------------------
- (void) writeToDefaults: (NSUserDefaults*) userDefaults
{
	// General
	[userDefaults setBool: confirmMoveToTrash  forKey: PREF_CONFIRM_TRASH];
	[userDefaults setBool: useAppleRemote  forKey: PREF_USE_APPLE_REMOTE];
	[userDefaults setInteger: startupWindowType  forKey: PREF_STARTUP_WINDOW_TYPE];
	[userDefaults setInteger: startupFolderType  forKey: PREF_STARTUP_FOLDER_TYPE];
	[userDefaults setObject: startupFolder  forKey: PREF_STARTUP_FOLDER];

	// Browser
	[userDefaults setBool: regenerateThumbnails  forKey: PREF_REGEN_THUMBNAILS];
	[userDefaults setBool: markGeneratedThumbnails  forKey: PREF_MARK_GEN_THUMBNAILS];
	[userDefaults setBool: showHiddenFolders  forKey: PREF_BROWSER_SHOW_HIDDEN];
	putColorInPreferences(userDefaults, PREF_BROWSER_BACK_COLOR, browserBackgroundColor);
	putColorInPreferences(userDefaults, PREF_BROWSER_TEXT_COLOR, browserTextColor);

	// Viewer
	[userDefaults setBool: useSingleWindow  forKey: PREF_VIEWER_SINGLE_WINDOW];
	[userDefaults setBool: useMovieNaturalSize  forKey: PREF_VIEWER_MOVIE_SIZE];
	[userDefaults setInteger: pageJump  forKey: PREF_VIEWER_PAGE_JUMP];
	[userDefaults setInteger: thumbStripState  forKey: PREF_VIEWER_THUMB_STRIP_STATE];
	[userDefaults setInteger: pathElements  forKey: PREF_VIEWER_PATH_ELEMENTS];
	putColorInPreferences(userDefaults, PREF_VIEWER_BACK_COLOR, viewerBackgroundColor);
	putColorInPreferences(userDefaults, PREF_VIEWER_TEXT_COLOR, viewerTextColor);

	// Slideshow
	[userDefaults setInteger: slideshowDelay	forKey: PREF_SLIDESHOW_DELAY];
	[userDefaults setBool:slideshowOrder		forKey: PREF_SLIDESHOW_ORDER];
	putColorInPreferences(userDefaults, PREF_SLIDESHOW_BACK_COLOR, slideshowBackgroundColor);
	putColorInPreferences(userDefaults, PREF_SLIDESHOW_TEXT_COLOR, slideshowTextColor);
	[userDefaults setObject: slideshowFolder  forKey: PREF_SLIDESHOW_FOLDER];
}

//------------------------------------------------------------------------------
- (BOOL) confirmMoveToTrash
{
	return confirmMoveToTrash;
}

//------------------------------------------------------------------------------
- (BOOL) useAppleRemote
{
	return useAppleRemote;
}

//------------------------------------------------------------------------------
- (NSString*) lastFolder
{
	return lastFolder;
}

//------------------------------------------------------------------------------
- (int) startupWindowType
{
	return startupWindowType;
}

//------------------------------------------------------------------------------
- (int) startupFolderType
{
	return startupFolderType;
}

//------------------------------------------------------------------------------
- (NSString*) startupFolder
{
	return startupFolder;
}

//------------------------------------------------------------------------------
- (void) setConfirmMoveToTrash:(BOOL) confirm
{
	confirmMoveToTrash = confirm;
}

//------------------------------------------------------------------------------
- (void) setUseAppleRemote:(BOOL) use
{
	useAppleRemote = use;
}

//------------------------------------------------------------------------------
- (void) setLastFolder:(NSString*) folder
{
	[lastFolder release];

	lastFolder = folder;
	[lastFolder retain];
}

//------------------------------------------------------------------------------
- (void) setStartupWindowType:(int) windowType
{
	startupWindowType = windowType;
}

//------------------------------------------------------------------------------
- (void) setStartupFolderType:(int) folderType
{
	startupFolderType = folderType;
}

//------------------------------------------------------------------------------
- (void) setStartupFolder:(NSString*) folder
{
	[startupFolder release];

	startupFolder = folder;
	[startupFolder retain];
}


//------------------------------------------------------------------------------
- (BOOL) regenerateMismatchedThumbs
{
	return regenerateThumbnails;
}

//------------------------------------------------------------------------------
- (BOOL) markGeneratedThumbnails
{
	return markGeneratedThumbnails;
}

//------------------------------------------------------------------------------
- (NSColor*) browserBackgroundColor
{
	return browserBackgroundColor;
}

//------------------------------------------------------------------------------
- (BOOL) showHiddenFolders
{
	return showHiddenFolders;
}

//------------------------------------------------------------------------------
- (NSColor*) browserTextColor
{
	return browserTextColor;
}

//------------------------------------------------------------------------------
- (void) setRegenerateMismatchedThumbs:(BOOL) regenerate
{
	regenerateThumbnails = regenerate;
}

//------------------------------------------------------------------------------
- (void) setMarkGeneratedThumbnails:(BOOL) mark
{
	markGeneratedThumbnails = mark;
}

//------------------------------------------------------------------------------
- (void) setBrowserBackgroundColor:(NSColor*) color
{
	[browserBackgroundColor release];

	browserBackgroundColor = color;
	[browserBackgroundColor retain];
}

//------------------------------------------------------------------------------
- (void) setShowHiddenFolders:(BOOL) show
{
	showHiddenFolders = show;
}

//------------------------------------------------------------------------------
- (void) setBrowserTextColor:(NSColor*) color
{
	[browserTextColor release];

	browserTextColor = color;
	[browserTextColor retain];
}



//------------------------------------------------------------------------------
- (int) pageJumpSize
{
	return pageJump;
}

//------------------------------------------------------------------------------
- (BOOL) useSingleWindow
{
	return useSingleWindow;
}

//------------------------------------------------------------------------------
- (BOOL) useMovieNaturalSize
{
	return useMovieNaturalSize;
}

//------------------------------------------------------------------------------
- (int) thumbStripState
{
	return thumbStripState;
}

//------------------------------------------------------------------------------
- (int) pathElements
{
	return pathElements;
}

//------------------------------------------------------------------------------
- (NSColor*) viewerBackgroundColor
{
	return viewerBackgroundColor;
}

//------------------------------------------------------------------------------
- (NSColor*) viewerTextColor
{
	return viewerTextColor;
}

//------------------------------------------------------------------------------
- (void) setPageJumpSize:(int) size
{
	pageJump = size;
}

//------------------------------------------------------------------------------
- (void) setThumbStripState:(int) state
{
	thumbStripState = state;
}

//------------------------------------------------------------------------------
- (void) setPathElements:(int) elements
{
	pathElements = elements;
}

//------------------------------------------------------------------------------
- (void) setUseSingleWindow:(BOOL) single
{
	useSingleWindow = single;
}

//------------------------------------------------------------------------------
- (void) setUseMovieNaturalSize:(BOOL) naturalSize
{
	useMovieNaturalSize = naturalSize;
}

//------------------------------------------------------------------------------
- (void) setViewerBackgroundColor:(NSColor*) color
{
	[viewerBackgroundColor release];

	viewerBackgroundColor = color;
	[viewerBackgroundColor retain];
}

//------------------------------------------------------------------------------
- (void) setViewerTextColor:(NSColor*) color
{
	[viewerTextColor release];

	viewerTextColor = color;
	[viewerTextColor retain];
}


//------------------------------------------------------------------------------
- (int) slideshowDelay
{
	return slideshowDelay;
}

//------------------------------------------------------------------------------
- (int) slideshowOrder
{
	return slideshowOrder;
}

//------------------------------------------------------------------------------
- (NSColor*) slideshowBackgroundColor
{
	return slideshowBackgroundColor;
}

//------------------------------------------------------------------------------
- (NSColor*) slideshowTextColor
{
	return slideshowTextColor;
}

//------------------------------------------------------------------------------
- (NSString*) slideshowFolder
{
	return slideshowFolder;
}

//------------------------------------------------------------------------------
- (void) setSlideshowDelay:(int) delay
{
	slideshowDelay = delay;
}

//------------------------------------------------------------------------------
- (void) setSlideshowOrder:(int) order
{
	slideshowOrder = order;
}

//------------------------------------------------------------------------------
- (void) setSlideshowBackgroundColor:(NSColor*) color
{
	[slideshowBackgroundColor release];

	slideshowBackgroundColor = color;
	[slideshowBackgroundColor retain];
}

//------------------------------------------------------------------------------
- (void) setSlideshowTextColor:(NSColor*) color
{
	[slideshowTextColor release];

	slideshowTextColor = color;
	[slideshowTextColor retain];
}

//------------------------------------------------------------------------------
- (void) setSlideshowFolder:(NSString*) folder
{
	[slideshowFolder release];
	slideshowFolder = folder;
	[slideshowFolder retain];
}

@end
